<?php
//模型
class AssetsModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'assets';
	}
	
	function get_list($arr,$page=1,$size=10){
		
		$where = $this->get_wheres($arr);
		
		
		$res = $this->_where_get_list($where,$page,$size);
		return $res;
	}
	
	function get_all($arr){
		
		$where = $this->get_wheres($arr);
		
		$res = $this->_where_get_list($where,1,1000);
		return $res['list'];
	}
	
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	
	
	function get_wheres($arr=array()){
		$where = " 1 ";
		if(isset($arr['date_month']) && !empty($arr['date_month'])){
			$where .= " AND date_month ='{$arr['date_month']}'";
		}
		
		if(isset($arr['region']) && !empty($arr['region'])){
			$where .= " AND region ='{$arr['region']}'";
		}
		if(isset($arr['company']) && !empty($arr['company'])){
			$where .= " AND company ='{$arr['company']}'";
		}
		if(isset($arr['bumen']) && !empty($arr['bumen'])){
			$where .= " AND bumen ='{$arr['bumen']}'";
		}
		if(isset($arr['subject']) && !empty($arr['subject'])){
			$where .= " AND subject ='{$arr['subject']}'";
			//把科目代码先去掉再查询
			/*preg_match_all('/[\x{4e00}-\x{9fa5}]+/u',$arr['subject'],$str);
			$strs = $str[0][0];
			if($str != $arr['subject']){
				$where .= " AND (subject ='{$arr['subject']}' OR subject ='{$strs}')";
			}else{
				$where .= " AND subject ='{$arr['subject']}'";
			}*/
			
		}
		if(isset($arr['bz'])){
			if($arr['bz']=='人民币'){
				$where .= " AND (bz ='{$arr['bz']}' OR bz ='')";
			}elseif($arr['bz']=='all'){

			}
			else{
				$where .= " AND bz ='{$arr['bz']}'";
			}
		}
		if(isset($arr['keyword']) && !empty($arr['keyword'])){
			$where .= " AND name LIKE '%{$arr['keyword']}%'";
		}
		if(isset($arr['user_id']) && !empty($arr['user_id'])){
			$where .= " AND `user_id` ='{$arr['user_id']}'";
		}
		
		if(isset($arr['type']) && !empty($arr['type']) && $arr['type']>=0){
			$where .= " AND `type` ='{$arr['type']}'";
		}
		
		if(isset($arr['start']) && isset($arr['end'])){
			if($arr['start'] == $arr['end']){
				$where .= " AND date(date) ='{$arr['start']}' ";
			}else{
				//$where .= " AND `date` >='{$arr['start']}' AND `date` <= '{$arr['end']}' ";
				$where .= " AND (date(date) between '{$arr['start']}' AND '{$arr['end']}')";
			}
		}
		if(isset($arr['status']) && !empty($arr['status'])){
			$where .= " AND status='{$arr['status']}'";
		}

		if(isset($arr['key_val']) && !empty($arr['key_val'])){
			$where .= " AND (assets_no LIKE '%{$arr['key_val']}%' OR name LIKE '%{$arr['key_val']}%')";
		}

		if(isset($arr['aid']) && !empty($arr['aid'])){
			$ids = implode(',', $arr['aid']);
			$where .= " AND aid in ({$ids})";
		}
		
		if(isset($arr['useless_date']) && !empty($arr['useless_date'])){
			$where .= " AND (useless_date >= {$arr['useless_date']} OR useless_date = 0)";
		}

		//用于报表查询折旧率的统计
		if(isset($arr['cur_date']) && !empty($arr['cur_date'])){
			$where .= " AND (DATE_FORMAT( start_date, '%Y-%m' ) <= '{$arr['cur_date']}' AND DATE_FORMAT( end_date, '%Y-%m' ) >= '{$arr['cur_date']}')";
		}
		
		return $where;
	}
	
	//引入数据
	function yinru_data($arr=array()){
		$where = $this->get_wheres($arr);
		
		$time = time();
		$appends = array(
			'addtime'=>$time
		);
		$base_fields = array('aid','company','region','bumen','bumen_scale','subject','money','bz');
		
		$select_field = array();
		$insert_field = array();
		
		foreach($base_fields as $val){
			$select_field[] = "`{$val}`";
			$insert_field[] = "`{$val}`";
			
		}
		$insert_field[0] = "`id`";
		
		foreach($appends as $key=>$val){
			$select_field[] = "`{$key}`";
			$insert_field[] = "'{$val}'";
		}
		
		
		//获取会计的账目
		//$select_sql = "select ".implode(',',$insert_field)." from ".$this->db->dbprefix."accounting where {$where}";
		//这里添加了个aid字段，可以防止重复添加相同数据
		$select_sql = "select ".implode(',',$insert_field)." from ".$this->db->dbprefix."accounting where {$where} and not exists (select aid from ".$this->db->dbprefix.$this->table." where aid=".$this->db->dbprefix."accounting.id)";
		
		$insert_sql = "INSERT INTO ".$this->db->dbprefix.$this->table." (".implode(',',$select_field).") {$select_sql} ";
		
		$query = $this->db->query($insert_sql);
		return 1;
	}

	//获取
	function get_date_assets($arr=array()){
		$where = $this->get_wheres($arr);

		$sql = "select id,company,region,bumen,start_date,useless_date,money,bz,depreciation,status from ".$this->db->dbprefix.$this->table." where {$where}";
		//return $sql;
		$query = $this->db->query($sql);
		$list = $query->result_array();
		return $list;
	}
}

